<html>
<head><meta charset="utf-8"><title>Auto importing traits · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html">Auto importing traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="187868565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187868565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187868565">(Feb 10 2020 at 23:09)</a>:</h4>
<p>Is there any sane way to get a collection of <code>ModuleDef::Trait</code> that correspond to a given hir <code>Adt</code>?<br>
Or maybe a way to get a collection of <code>ModuleDef::Trait</code> that have a hir <code>Function</code> defined in them?</p>
<p>In other words, I want to either find all traits that are implemented for a given <code>Adt</code> or find all traits that have the particular function definition.</p>
<p>One strong limitation that makes it <em>fun</em>: the trait is not in the current scope (i.e. no <code>use</code> statement for it from the place we perform a search hence no <code>SourceAnalyzer::resolve_path</code> can be used).</p>
<p>So far I've found an eerie way to get that as </p>
<p><code>ImplBlock::all_in_crate</code> <br>
-&gt; filter out the ones that target the given <code>Adt</code> only<br>
-&gt; get the <code>ra_hir_def::TypeRef</code> via <code>ImplBlock::target_trait</code> on the remaining <br>
-&gt; scrape the trait name out of it (by considering the <code>TypeRef::Path</code> variants only) <br>
-&gt; do <code>ImportsLocator::find_imports</code> on the name</p>
<p>Can there be a better way?<br>
It would be great to get the <code>Trait</code> instead of <code>TypeRef</code> somehow at least.</p>



<a name="187891031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187891031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187891031">(Feb 11 2020 at 08:08)</a>:</h4>
<p>Yeah, I wouldn't recommend that approach <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> This needs to use the <a href="https://github.com/rust-analyzer/rust-analyzer/blob/f8d6d6f23bfb15021be91031ba983da19f0d3ada/crates/ra_hir_ty/src/method_resolution.rs#L182" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/f8d6d6f23bfb15021be91031ba983da19f0d3ada/crates/ra_hir_ty/src/method_resolution.rs#L182">method resolution code</a> and tell it to consider all traits, not just the ones in scope (that option doesn't exist yet)</p>



<a name="187891839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187891839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187891839">(Feb 11 2020 at 08:27)</a>:</h4>
<p>I see that the resolution method boils down to this: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir_ty/src/method_resolution.rs#L385" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir_ty/src/method_resolution.rs#L385">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir_ty/src/method_resolution.rs#L385</a></p>
<p>And if I get it right, the method just adds the traits in scope to the iteration, that's it.<br>
So, in order to make it to consider all traits, I have to get all traits first, haven't I?</p>
<p>How do I do that?<br>
I wonder why do I need to change this method, if I'm able to get all traits? :)</p>



<a name="187892027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187892027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187892027">(Feb 11 2020 at 08:31)</a>:</h4>
<p>You're right, you don't need to change it since it takes the set of traits as a parameter nowadays</p>



<a name="187895170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187895170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187895170">(Feb 11 2020 at 09:25)</a>:</h4>
<p>I think to get the set of candidate traits the best way would be to go through the <code>CrateDefMap</code> of the current crate and its direct dependencies, and collect the set of all traits in all modules in there. (We might want to make an index of that, but I guess it's better to actually first try the brute-force way)</p>



<a name="187896487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187896487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187896487">(Feb 11 2020 at 09:43)</a>:</h4>
<p>Thanks, I'll try that.</p>



<a name="187899611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187899611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187899611">(Feb 11 2020 at 10:28)</a>:</h4>
<p>I think the right approach here is to filter by function first (using the symbol index)</p>



<a name="187899650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187899650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187899650">(Feb 11 2020 at 10:28)</a>:</h4>
<p>I hope we can live without "find all traits for a type", as that doesn't look like it can scale.</p>



<a name="187900091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187900091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187900091">(Feb 11 2020 at 10:34)</a>:</h4>
<p>method resolution filters by the function first anyway</p>



<a name="187900119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187900119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187900119">(Feb 11 2020 at 10:35)</a>:</h4>
<p>but getting all traits with a function with a certain name via an index would of course be nicer</p>



<a name="187909009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187909009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187909009">(Feb 11 2020 at 13:03)</a>:</h4>
<p>If there's a way to get to get impls for a given <code>Trait</code> or <code>TraitDef</code>, I can avoid using the <code>method_resolution</code> method at all.<br>
I think I should, because otherwise I need to somehow make it available in <code>ra_assists</code> crate which does not make much sense to me.</p>



<a name="187909188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187909188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187909188">(Feb 11 2020 at 13:06)</a>:</h4>
<p>And I think there is a way via <code>ImplBlock::for_trait</code>, so all good.</p>



<a name="187909812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187909812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187909812">(Feb 11 2020 at 13:14)</a>:</h4>
<p>you have to use method resolution, otherwise you will get wrong and incomplete results</p>



<a name="187910019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187910019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187910019">(Feb 11 2020 at 13:17)</a>:</h4>
<p>actually, we already expose it: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1025" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1025">https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1025</a></p>



<a name="187910862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187910862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187910862">(Feb 11 2020 at 13:29)</a>:</h4>
<p>Well, this method does not find me any candidates for my tiny test.<br>
I'll try to find out why, hope it does not depend on the scope somehow.</p>



<a name="187912374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912374">(Feb 11 2020 at 13:48)</a>:</h4>
<p>I think that happens because of this: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1048" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1048">https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1048</a></p>
<p>Currently I'm trying to import traits for the associated trait functions:</p>
<div class="codehilite"><pre><span></span>mod test_mod {
    pub trait TestTrait {
        fn test_function();
    }
    pub struct TestStruct {}
    impl TestTrait for TestStruct {
        fn test_function() {}
    }
}

fn main() {
    test_mod::TestStruct::test_function&lt;|&gt;
}
</pre></div>


<p>and all my traits get rejected here: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir_ty/src/method_resolution.rs#L454" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir_ty/src/method_resolution.rs#L454">https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir_ty/src/method_resolution.rs#L454</a></p>



<a name="187912444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912444">(Feb 11 2020 at 13:49)</a>:</h4>
<p>If I replace the <code>LookupMode</code> with the other variant, I get something passed into the closure.</p>
<p>So, should we expose another method? Or rather allow to specify the <code>LookupMode</code> as a parameter?<br>
Or am I doing something wrong?</p>



<a name="187912588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912588">(Feb 11 2020 at 13:51)</a>:</h4>
<p>for a path like that, you need <code>LookupMode::Path</code>, yeah</p>



<a name="187912686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912686">(Feb 11 2020 at 13:52)</a>:</h4>
<p>we should maybe expose another variant that uses <code>LookupMode::Path</code> and also supports returning associated constants</p>



<a name="187912747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912747">(Feb 11 2020 at 13:53)</a>:</h4>
<p>maybe call it <code>iterate_associated_candidates</code> or something like that</p>



<a name="187912772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912772">(Feb 11 2020 at 13:53)</a>:</h4>
<p>(I was assuming you're doing method calls like <code>foo.bar()</code>)</p>



<a name="187912774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912774">(Feb 11 2020 at 13:54)</a>:</h4>
<p>Sounds reasonable, not sure how to pass the <code>AssocItemId::ConstId</code> into that callback though.<br>
I'll try something.</p>



<a name="187912835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187912835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187912835">(Feb 11 2020 at 13:54)</a>:</h4>
<p>we have an <code>enum AssocItem</code> in <code>code_model</code></p>



<a name="187913250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187913250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187913250">(Feb 11 2020 at 13:59)</a>:</h4>
<p>Oh wait, there's already a method that does it :)<br>
Thanks for the guidance anyway, that helps a lot.</p>



<a name="187914026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187914026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187914026">(Feb 11 2020 at 14:08)</a>:</h4>
<p>Ok, one last set of questions: how is the <a href="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1056" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1056">https://github.com/rust-analyzer/rust-analyzer/blob/3c98681d4efeabe91dc91ff3ef0e1ce11c35d067/crates/ra_hir/src/code_model.rs#L1056</a> method supposed to help me to detect the <em>traits</em> I want to import?</p>
<p>The callback signature is <code>mut callback: impl FnMut(&amp;Ty, AssocItem) -&gt; Option&lt;T&gt;,</code> so it does not return a trait.<br>
Or is the plan to call the method for each trait I've located before, one by one, and see if something gets passed into the callback for that trait?<br>
I cannot even fully check what I get back, because <code>Ty</code> is not accesssible in <code>ra_assists</code> and checking <code>AssocItem</code> seem to make no sense since we've filtered out the traits by the method before already.</p>



<a name="187914168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187914168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187914168">(Feb 11 2020 at 14:10)</a>:</h4>
<p>And do I get it right, that <code>ImplBlock::for_trait</code> approach is worse than the way you've proposed because it does not consider derefs?</p>



<a name="187918623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187918623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187918623">(Feb 11 2020 at 14:53)</a>:</h4>
<p>once you have the <code>AssocItem</code>, you can get its container, which is the trait</p>



<a name="187918662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187918662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187918662">(Feb 11 2020 at 14:53)</a>:</h4>
<p>It is not, that's the trick, it's the <code>Function</code>.</p>



<a name="187918760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187918760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187918760">(Feb 11 2020 at 14:54)</a>:</h4>
<p>for a <code>Function</code> as well you can get the container. you might need to add methods for it in <code>code_model</code>, but it's straightforward</p>



<a name="187918781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187918781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kirill Bulatov <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187918781">(Feb 11 2020 at 14:54)</a>:</h4>
<p>Ah, silly me, got it. Thanks.</p>



<a name="187918951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Auto%20importing%20traits/near/187918951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Auto.20importing.20traits.html#187918951">(Feb 11 2020 at 14:56)</a>:</h4>
<blockquote>
<p>And do I get it right, that <code>ImplBlock::for_trait</code> approach is worse than the way you've proposed because it does not consider derefs?</p>
</blockquote>
<p>derefs aren't relevant for path-style method calls, but to decide whether a type implements a trait you have to use chalk, you can't just look at the impls. consider impls like <code>impl&lt;T&gt; Trait for T where T: SomeOtherTrait</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>